package com.lanlian.cayman.dialog;

import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.example.utils.DrawStyleUtil;
import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.KLineHengPingActivity;
import com.scwang.smartrefresh.layout.util.DensityUtil;

/**
 * <pre>
 *     date   : 2018/8/22 20:09
 *     desc   : 粗细 线样式选择选
 *     version: 1.0
 * </pre>
 */
public class StylePopup implements View.OnClickListener {

    private KLineHengPingActivity mContext;

    PopupWindow window;

    int color;

    private LinearLayout mLl1;
    private View mV1;
    private LinearLayout mLl2;
    private View mV2;
    private LinearLayout mLl3;
    private View mV3;
    private LinearLayout mLl4;
    private View mV4;
    private LinearLayout mLl5;
    private View mV5;
    private LinearLayout mLl6;
    private View mV6;
    private LinearLayout mLl7;
    private DashLineView mV7;
    private LinearLayout mLl8;
    private DashLineView mV8;





    public StylePopup(Activity mContext)
   {
       this.mContext=(KLineHengPingActivity)mContext;
       initView();
   }
   public void showPopup(View view)
   {
       DensityUtil du=new DensityUtil();
       int width= du.dip2px(100);

       int height=du.dip2px(44+20);


       window.showAtLocation(view,Gravity.TOP|Gravity.RIGHT,width,height);
   }

    private void initView() {
        LinearLayout ll=new LinearLayout(mContext);
        // 用于PopupWindow的View
        View view= LayoutInflater.from(mContext).inflate(R.layout.dialog_styleselect, ll, false);
        DensityUtil du=new DensityUtil();
        int width= du.dip2px(350);
        int heiht= du.dip2px(250);
        window=new PopupWindow(view, width, heiht, true);
        // 设置PopupWindow是否能响应外部点击事件
        window.setOutsideTouchable(false);
        // 设置PopupWindow是否能响应点击事件
        window.setTouchable(true);


        mLl1 = view.findViewById(R.id.ll1);
        mV1 = view.findViewById(R.id.v1);
        mLl2 = view.findViewById(R.id.ll2);
        mV2 = view.findViewById(R.id.v2);
        mLl3 = view.findViewById(R.id.ll3);
        mV3 = view.findViewById(R.id.v3);
        mLl4 = view.findViewById(R.id.ll4);
        mV4 = view.findViewById(R.id.v4);
        mLl5 = view.findViewById(R.id.ll5);
        mV5 = view.findViewById(R.id.v5);
        mLl6 = view.findViewById(R.id.ll6);
        mV6 = view.findViewById(R.id.v6);
        mLl7 = view.findViewById(R.id.ll7);
        mV7 = view.findViewById(R.id.v7);
        mLl8 = view.findViewById(R.id.ll8);
        mV8 = view.findViewById(R.id.v8);

        setListen(mLl1);
        setListen(mLl2);
        setListen(mLl3);
        setListen(mLl4);
        setListen(mLl5);
        setListen(mLl6);
        setListen(mLl7);
        setListen(mLl8);




        //color=DrawStyleUtil.getColor();
        color=mContext.kline.kLineView.styleView.getColor();
        //初始化线的宽度
       // int width1=DrawStyleUtil.getWidth();
        int width1=mContext.kline.kLineView.styleView.getWidth();

        if(width1==2)
        {
            setSelect(0);
        }
        else if(width1==3)
        {
            setSelect(1);
        }
        else if(width1==4)
        {
            setSelect(2);
        }
        else if(width1==5)
        {
            setSelect(3);
        }
        else if(width1==6)
        {
            setSelect(4);
        }
        else
        {
            setSelect(0);
        }
        //初始化虚线的间距
        //int width2=DrawStyleUtil.getXuxian_width();
       int width2=mContext.kline.kLineView.styleView.getXuxian_width();
        if(width2==0)
        {
            setSelect2(5);
        }
        else if(width2==8)
        {
            setSelect2(6);
        }
        else if(width2==12)
        {
            setSelect2(7);
        }
        else
        {
            setSelect2(5);
        }

    }
    private void init1()
    {
        int color=mContext.getResources().getColor(R.color.color_back_drawutil);
        int color2=mContext.getResources().getColor(R.color.color_style_back2);
        mLl1.setBackgroundColor(color);
        mLl2.setBackgroundColor(color);
        mLl3.setBackgroundColor(color);
        mLl4.setBackgroundColor(color);
        mLl5.setBackgroundColor(color);



        mV1.setBackgroundColor(color2);
        mV2.setBackgroundColor(color2);
        mV3.setBackgroundColor(color2);
        mV4.setBackgroundColor(color2);
        mV5.setBackgroundColor(color2);




    }
    private void init2()
    {
        int color1=mContext.getResources().getColor(R.color.color_style_back3);
        int color2=mContext.getResources().getColor(R.color.color_style_back2);
        mLl6.setBackgroundColor(color1);
        mLl7.setBackgroundColor(color1);
        mLl8.setBackgroundColor(color1);

        mV6.setBackgroundColor(color2);
        mV7.setColor(color2);
        mV8.setColor(color2);

        mV8.setLinewidth(12);
    }

    /**
     * 选择线的宽度
     * @param index
     */
    private void setSelect(int index)
    {
        int color2=mContext.getResources().getColor(R.color.color_line_drawutil);
        init1();
        if(index==0)
        {
            mLl1.setBackgroundColor(color2);
            mV1.setBackgroundColor(color);
        }
        else if(index==1)
        {
            mLl2.setBackgroundColor(color2);
            mV2.setBackgroundColor(color);
        }
        else if(index==2)
        {
            mLl3.setBackgroundColor(color2);
            mV3.setBackgroundColor(color);
        }
        else if(index==3)
        {
            mLl4.setBackgroundColor(color2);
            mV4.setBackgroundColor(color);
        }
        else if(index==4)
        {
            mLl5.setBackgroundColor(color2);
            mV5.setBackgroundColor(color);
        }





    }

    /**
     * 选择虚线
     * @param index
     */
    private  void setSelect2(int index)
    {
        int color2=mContext.getResources().getColor(R.color.color_line_drawutil);
        init2();
        if(index==5)
        {
            mLl6.setBackgroundColor(color2);
            mV6.setBackgroundColor(color);
        }
        else if(index==6)
        {
            mLl7.setBackgroundColor(color2);
            mV7.setColor(color);
        }
        else if(index==7)
        {
            mLl8.setBackgroundColor(color2);
            mV8.setColor(color);
        }
    }
    public void setListen(View view)
    {
         view.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 if(v==mLl1)
                 {
                     setSelect(0);
                     //DrawStyleUtil.setWidth(2);
                     mContext.kline.kLineView.styleView.setWidth(2);
                 }
                 else if(v==mLl2)
                 {
                     setSelect(1);
                     //DrawStyleUtil.setWidth(3);
                     mContext.kline.kLineView.styleView.setWidth(3);
                 }
                 else if(v==mLl3)
                 {
                     setSelect(2);
                    // DrawStyleUtil.setWidth(4);
                     mContext.kline.kLineView.styleView.setWidth(4);
                 }
                 else if(v==mLl4)
                 {
                     setSelect(3);
                     //DrawStyleUtil.setWidth(5);
                     mContext.kline.kLineView.styleView.setWidth(5);
                 }
                 else if(v==mLl5)
                 {
                     setSelect(4);
                     //DrawStyleUtil.setWidth(6);
                     mContext.kline.kLineView.styleView.setWidth(6);
                 }


                 if(v==mLl6)
                 {
                     setSelect2(5);
                     //DrawStyleUtil.setXuxian_width(0);
                     mContext.kline.kLineView.styleView.setXuxian_width(0);
                 }
                 else if(v==mLl7)
                 {
                     setSelect2(6);
                    // DrawStyleUtil.setXuxian_width(8);
                     mContext.kline.kLineView.styleView.setXuxian_width(8);
                 }
                 else if(v==mLl8)
                 {
                     setSelect2(7);
                    // DrawStyleUtil.setXuxian_width(12);
                     mContext.kline.kLineView.styleView.setXuxian_width(12);
                 }
                 mContext.kline.kLineView.invalidate();
             }
         });
    }


    @Override
    public void onClick(View v)
    {
        Intent intent=new Intent();
        int id = v.getId();
        switch (id)
        {
            case R.id.iv_close:

                break;
            default:
                break;

        }
    }












}
